package net.esj.auth.inst.struts2;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;

import net.esj.auth.AuthException;
import net.esj.auth.Constants;
import net.esj.auth.inst.struts2.GroupAction.CoreAuthSimpleGroup;
import net.esj.auth.manager.AuthManager;
import net.esj.auth.pojo.CoreAdmin;
import net.esj.auth.pojo.CoreAuthFunc;
import net.esj.auth.pojo.CoreAuthGroup;
import net.esj.auth.pojo.CoreAuthModel;
import net.esj.basic.core.view.support.vojo.BaseVojo;
import net.esj.basic.core.view.support.vojo.TransObject;
import net.esj.basic.core.view.tools.searcher.SearcherColumn;
import net.esj.basic.exception.MelonException;
import net.esj.basic.exception.MelonRuntimeException;
import net.esj.basic.utils.CollectionUtils;
import net.esj.basic.utils.Validators;
import net.esj.basic.utils.writer.WriterCode;

public class GroupAction extends AbstractAuthAction<CoreAuthGroup, CoreAuthSimpleGroup> implements Constants{

	private AuthManager authManager;
	
	private List<CoreAuthModel> models;
	
	private CoreAuthModel model;
	
	private String key;
	
	@Override
	public String list() {
		if(Validators.isEmpty(key)){
			models = authManager.allModels();
			if(!CollectionUtils.isEmpty(models)){
				id=models.get(1).getId();
			}
		}else{
			id = key;
		}
		return super.list();
	}
	@Override
	public void doQueryPage() {
		for(SearcherColumn column : pagination.getSearcher().getColumns()){
			switch(column.getKey()){
				case "model.id":
					if("CORE_ADMIN".equals(column.getValue())){
						CoreAdmin admin = getSessionUser(CoreAdmin.class);
						if(admin.getName().equals(CoreAdmin.SUPER_ADMINISTRATOR_NAME)){
							pagination.getQueryBuilder().eq("isDelete", 0);
						}else{
							List<String> list = new ArrayList<String>();
							list = authManager.getChildAdmin(admin.getId(), list);
							list.add(admin.getId());
							pagination.getQueryBuilder().eq("isDelete", 0).in("createId", list);
						}
					}
			}	
		}
		pagination = defaultService.queryPageByQueryBuilder(pagination,getListVoClass());
		
	}
	public void doAdd() throws MelonException{
			CoreAuthGroup group = (CoreAuthGroup) defaultService.translate(entity, entityClass);
			for(String func: entity.getFuncs()){
				if(!Validators.isEmpty(func)){
					CoreAuthFunc f = new CoreAuthFunc();
					f.setId(func);
					group.getFunctions().add(f);
				}
			}
			if(getNamespace().indexOf("backstage")>0){
				CoreAdmin admin = getSessionUser(CoreAdmin.class);
				group.setCreateId(admin.getId());
			}
			defaultService.save(group);
			
	}
	
	
	@Override
	public void doUpdate() {
		CoreAuthGroup group = (CoreAuthGroup) defaultService.translate(entity, entityClass);
		for(String func: entity.getFuncs()){
			if(!Validators.isEmpty(func)){
				CoreAuthFunc f = new CoreAuthFunc();
				f.setId(func);
				group.getFunctions().add(f);
			}
		}
		if(getNamespace().indexOf("backstage")>0){
			CoreAdmin admin = getSessionUser(CoreAdmin.class);
			group.setCreateId(admin.getId());
		}
		defaultService.update(group);
	}


	public void load()throws MelonRuntimeException {
		if (Validators.isEmpty(id)) {
			return;
		}
		CoreAuthGroup group = defaultService.get(id, entityClass);
		entity = (CoreAuthSimpleGroup) defaultService.translate(group, voClass);
		entity.setFuncsSet(group.getFunctions());
		writeJson(entity,WriterCode.FORM);
	}
	
	public void doDelete(){
		try {
			for(String str : deletes.split(";")){
				CoreAuthGroup group = new CoreAuthGroup();
				group.setId(str);
				authManager.deleteGroup(group);
			}
		} catch (AuthException e) {
			throw new MelonException(e);
		}
	}
	
	public CoreAuthSimpleGroup getEntity() {
		return entity;
	}

	public void setEntity(CoreAuthSimpleGroup entity) {
		this.entity = entity;
	}

	@Inject
	public void setAuthManager(AuthManager authManager) {
		this.authManager = authManager;
	}

	public List<CoreAuthModel> getModels() {
		return models;
	}

	public CoreAuthModel getModel() {
		return model;
	}

	public void setModel(CoreAuthModel model) {
		this.model = model;
	}

	public static class CoreAuthSimpleGroup implements BaseVojo<String>,TransObject{
		
		private String id;
		private String name;
		private String descriptions;
		private CoreAuthModel model;
		private String[] funcs;
		private int isDelete;
		
		public int getIsDelete() {
			return isDelete;
		}
		public void setIsDelete(int isDelete) {
			this.isDelete = isDelete;
		}
		
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getDescriptions() {
			return descriptions;
		}
		public void setDescriptions(String descriptions) {
			this.descriptions = descriptions;
		}
		public String[] getFuncs() {
			return funcs;
		}
		public void setFuncs(String[] funcs) {
			this.funcs = funcs;
		}
		public void setFuncsStr(String funcs) {
			this.funcs = funcs.split(";");
		}
		
		public CoreAuthModel getModel() {
			return model;
		}
		public void setModel(CoreAuthModel model) {
			this.model = model;
		}
		public void setFuncsSet(Set<CoreAuthFunc> funcs){
			this.funcs = new String[funcs.size()];
			int i=0;
			for(CoreAuthFunc f: funcs){
				this.funcs[i] =f.getId();
				i++;
			}
		}
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

}
